package cz.possoft.client.four.gui.panel;

import cz.possoft.client.four.gui.components.FourPane;
import cz.possoft.client.gui.dialog.SimpleDialog;
import cz.possoft.client.gui.panel.GamePanel;
import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.generic.utils.GenericUtils;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;
import cz.possoft.serializable.four.game.FourGame;
import cz.possoft.serializable.four.game.FourPole;
import cz.possoft.serializable.generic.Game;
import cz.possoft.serializable.generic.Location;

/**
  * Hraci panel pro hur Four (ctyri v rade) 
  * 
  * @author mara
  */
public class FourPanel extends GamePanel {
	
	// ma v sobe pocet tahu, ktere byly odehrany. Pri odehrani 42 tahu hra konci remizou.
	private int tahCounter;
	
    /** Creates a new instance of FourPanel */
    public FourPanel() {    	
    }
    
    // inicializuje komponenty, ktere by mely byt specificke pro tuto hru.    
    protected void initComponents() {
    	super.initComponents();
    }  
    
    // Tato metoda zavola setGame a pote provede nezbytne inicialni
    // kroky pri nasetovani teto hry (prekresleni panelu atd.)    
    public void setAndInitGame(Game gam) {    
    	super.setAndInitGame(gam);    	
        FourGame game = (FourGame)gam;        
        if (game.isMeWhite()) {        	      	
        	getClientNameLabel().setText(addHtmlTagsToLabels(game.getWhitePlayerName(), "RED", '5'));        
        }
        if (!game.isMeWhite()) {        	
        	getOpponentNameLabel().setText(addHtmlTagsToLabels(game.getWhitePlayerName(), "RED", '5'));        
        }  
        getLastTahLabel().setVisible(false);
        tahCounter = 0;
        getGamePane().setHraciPole(game.getHraciPole());    	
    }    
    
    // nastavi active u hry a provede pridruzene akce
    public void setActiveToGame(boolean active) {
        super.setActiveToGame(active);	
    }
    
    // Odehrani tahu. Smysl maji jen oldX a oldY.
    // newX je identifikace barvy. newY se nepouziva a melo by byt pro tuto hru vzdy 0.
    public void doPlay(int oldX, int oldY, int newX, int newY) {    
    	super.doPlay(oldX, oldY, newX, newY);
    	((FourPole)getGamePane().getHraciPole()).getFigurka(oldX, oldY).setColor(newX);
    	((FourPane)(getGamePane())).startPadani(oldX, oldY, newX);
    	controlGameEnd();
    	addToCounterAndControlRemiza();    	
    	getGamePane().repaint();
    }	
    
    // Pro hrace ktery je na tahu je oznaceno misto, kam muze tahnout (hodit balonek)
    // Oznacuje se vzdycky aktualni sloupec
    public void updateForPlaceWhereIsMouse(Location location) {
    	((FourPane)getGamePane()).getRingFigurkaLocation().setX(location.getX());
    	((FourPane)getGamePane()).getRingFigurkaLocation().setY(location.getY());
    	((FourPane)getGamePane()).repaint();
    }
    
    // odehraje tah pro danou location. Location je vzdy
    // horni misto na sachovnici, takze tato metoda jeste musi zjistit
    // misto, kam balonek spadne.
    public void play(Location location) {    	
    	boolean prazdneMisto = false;
    	int yPrazdnehoMista = 6;
    	FourPole fourPole = (FourPole)getGamePane().getHraciPole();
    	while ((!prazdneMisto) && (yPrazdnehoMista > 0)) {
    		if (fourPole.getFigurka(location.getX(), yPrazdnehoMista).getColor() == GenericUtils.COLOR_NEUTRAL) {
    			prazdneMisto = true;
    		}
    		else {
    			yPrazdnehoMista--;
    		}
    	}
    	// Pole uz je zaplneno do vysky, takze v tomto sloupci nelze hrat.
    	if (yPrazdnehoMista == 0) {
    		return;
    	}
    	int color = getGame().isMeWhite() ? GenericUtils.COLOR_WHITE : GenericUtils.COLOR_BLACK;
    	NastaveniShowPanel.getConnectThread().sendToServer("PLAY: _" + location.getX() + "_" + yPrazdnehoMista + "_" + color + "_0");
     	((FourPane)getGamePane()).getRingFigurkaLocation().setNull();    	   
    	doPlay(location.getX(), yPrazdnehoMista, color, 0);
    }  
    
    // prida do poctu tahu jedna a v pripade, ze jsou vsechna pole plna - odehrano 42 tahu - 
    // hra konci.
    private void addToCounterAndControlRemiza() {
    	tahCounter++;
    	if (tahCounter == 42) {
    		NastaveniShowPanel.getConnectThread().doRemiza();
    	}
    }
    
    // zkontroluje jestli je hra v koncovem stavu.
    // tzn. jestli jsou nekde 4 v rade. Pokud jo, tak provede
    // prislusne ukoncujici akce
    private void controlGameEnd() {
    	int flag = (((FourPole)(getGame().getHraciPole())).controlGameEnd());
    	// pripad kdy je hra ukoncena
    	if (flag != GenericUtils.COLOR_NEUTRAL) {
           	PanelHolderUtils.getCurrentGamePanel().setActiveToGame(false);
        	PanelHolderUtils.getCurrentGamePanel().setEndStateToGame(true);
        	if (((getGame().isMeWhite()) && (flag == GenericUtils.COLOR_WHITE)) || ((!getGame().isMeWhite()) && (flag == GenericUtils.COLOR_BLACK))) {
                SimpleDialog.getInstance(PropertiesManager.getInstance().getMessageForKey(Text.VYHRA), Text.getPiskvorkyMessage("4", true));    		
        	}
        	else {
        		SimpleDialog.getInstance(PropertiesManager.getInstance().getMessageForKey(Text.PROHRA), Text.getPiskvorkyMessage("4", false));  
        	}
    	}    	
    }

}
